package com.hc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hc.common.bean.PageBean;
import com.hc.domain.*;
import com.hc.mapper.*;
import com.hc.query.ProjVO;
import com.hc.res.AuthorListDTO;
import com.hc.res.CategoryDTO;
import com.hc.res.ProjListItemDTO;
import com.hc.res.ProjsDTO;
import com.hc.service.ProjService;
import com.hc.trans.AuthorTrans;
import com.hc.trans.CategoryTrans;
import com.hc.trans.ProjTrans;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: hecai
 * @date: 2021/11/9 11:17
 * @description:
 */
@Service
@SuppressWarnings("all")
public class ProjServiceImpl implements ProjService {

    @Resource
    private ProjMapper projMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProjCategoryMapper projCategoryMapper;
    @Resource
    private AuthorMapper authorMapper;
    @Resource
    private ProjAuthorMapper projAuthorMapper;
    @Resource
    private TechnologyMapper technologyMapper;
    @Resource
    private ProjTechMapper projTechMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return projMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Proj proj,String[] categoryName, String[] technologyName,String names) {
            int insert = projMapper.insert(proj);
            if (insert==1){
                Author author1 = authorMapper.selectByName(names);
                if (ObjectUtils.isEmpty(author1)){
                    Author author = Author.builder().nickname(names).build();
                    // 添加作者.作者中间表
                    authorMapper.insert(author);
                    projAuthorMapper.insert(new ProjAuthor(proj.getId(), author.getId()));
                }else {
                    projAuthorMapper.insert(new ProjAuthor(proj.getId(), author1.getId()));
                }

                // 技术中间表
                List<Long> longs1 = technologyMapper.selectIdsByName(technologyName);
                for (Long aLong : longs1) {
                    projTechMapper.insert(new ProjTech(proj.getId(),aLong));
                }
                // 分类中间表
                List<Long> longs = categoryMapper.selectIdsByName(categoryName);
                for (Long aLong : longs) {
                    projCategoryMapper.insert(new ProjCategory(proj.getId(),aLong));
                }
                return 1;
            }
        return 0;
    }

    @Override
    public int insertSelective(Proj record) {
        return projMapper.insertSelective(record);
    }

    @Override
    public int batchInsert(List<Proj> list) {
        int num = 0;
        for(Proj proj:list){
            num += projMapper.insert(proj);
        }
        return num;
    }

    @Override
    public Proj selectByPrimaryKey(Long id) {
        return projMapper.selectByPrimaryKey(id);
    }

    @Override
    public ProjsDTO selectAllPrimaryKey(Long id) {
        Proj proj = projMapper.selectByPrimaryKey(id);
        List<Integer> list = projTechMapper.selectTidByPrimaryKey(id);

        ProjsDTO projsDTO = ProjTrans.MAPPER.ProjsDTO(proj);

        if(list.size()>0){
            List<String> techNameList=new ArrayList<>();
            for (Integer tid : list) {
                String name = technologyMapper.selectTidByPrimaryKey(tid.longValue());
                techNameList.add(name);
            }
            projsDTO.setTechNameList(techNameList);
        }
        List<Integer> listCid = projCategoryMapper.selectCidByPrimaryKey(id);
        if(listCid.size()>0){
            List<String>cateNameList=new ArrayList<>();
            for (Integer cid : listCid) {
                String name = categoryMapper.selectNameByPrimaryKey(cid.longValue());
                cateNameList.add(name);
            }
            projsDTO.setCateNameList(cateNameList);
        }
        List<Integer> listAid = projAuthorMapper.selectAidByPrimaryKey(id);
        if (listAid.size()>0) {
            List<String>authorNameList=new ArrayList<>();
            for (Integer aid : listAid) {
                String name = authorMapper.selectNameByPrimaryKey(aid.longValue());
                authorNameList.add(name);
            }
            projsDTO.setAuthorNameList(authorNameList);
        }
        return projsDTO;
    }

    @Override
    public int updateByPrimaryKeySelective(Proj record) {
        return projMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Proj record) {
        return projMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Proj> selectByName(String name,Integer status) {
        return projMapper.selectByName(name,status);
    }
//    @Override
//    public List<Proj> selectProjsByName(String name) {
//        List<Proj> projs = projMapper.selectByName(name);
//        for (Proj proj: projs) {
//            List<Integer> list = projAuthorMapper.selectAidByPrimaryKey(proj.getId());
//            ProjsDTO projsDTO = new ProjsDTO();
//            List<AuthorDTO> authorDTOList = new ArrayList<>();
//            for (Integer id : list) {
//                Author author = authorMapper.selectByPrimaryKey(id.longValue());
//                AuthorDTO authorDTO = new AuthorDTO();
//                authorDTO.builder()
//                        .id(author.getId())
//                        .nickname(author.getNickname())
//                        .build();
//                authorDTOList.add(authorDTO);
//
//            }
//            projsDTO.setAuthorNameList(authorDTOList);
//        }
//    }

    @Override
    public List<Proj> getAllProj(Integer category,String name) {
        return projMapper.getAllProj(category,name);
    }

    @Override
    public Proj getProjInfoById(Integer id, Integer category) {
        return projMapper.getProjInfoById(id, category);
    }

    @Override
    public List<Proj> getAllProjByCondition(ProjVO projVO) {
        return projMapper.getAllProjByCondition(projVO);
    }



    @Override
    public List<CategoryDTO> selectProjsByCategory(String name) {
        //顶级类别
        final List<Category> topCategoryList = categoryMapper.selectCategoryByPid(0);
        final List<CategoryDTO> topCategoryDTOList = CategoryTrans.MAPPER.categoryList2CategoryDTOList(topCategoryList);

        for (CategoryDTO topCategoryDTO : topCategoryDTOList) {
            //顶级类别下的子类(假设整个系统只有两层类别，否则此处需要使用递归)
            final List<Category> sunCategoryList = categoryMapper.selectCategoryByPid(topCategoryDTO.getId());

            //用来放置顶级类别下所有子类别对应的项目的编号
            List<Long> temp = new ArrayList<>();
            for (Category sunCategory : sunCategoryList) {
                //获取某个子类别下所有的Proj的编号
                final List<Long> projIds = projCategoryMapper.selectProjIdByCategoryId(sunCategory.getId());
                temp.addAll(projIds);
            }
            if (temp.size()>0){
                final List<Proj> projs = projMapper.selectProjsByIdRange(temp,name);
                List<ProjListItemDTO> projListDTOItemList = ProjTrans.MAPPER.projList2ProjListItemDTOList(projs);
                topCategoryDTO.setProjListDTOItemList(projListDTOItemList);
            }
        }
        for (CategoryDTO all : topCategoryDTOList) {
            List<ProjListItemDTO> allProj = all.getProjListDTOItemList();
            for (ProjListItemDTO proj : allProj) {
                ArrayList<AuthorListDTO> strings = new ArrayList<>();
                List<Integer> projAuthorIds = projAuthorMapper.getProjAuthorIds(proj.getId());
                for (Integer projAuthorId : projAuthorIds) {
                    AuthorListDTO authorListDTO = AuthorTrans.MAPPER.author2AuthorListDTO(authorMapper.selectByPrimaryKey(projAuthorId.longValue()));
                    strings.add(authorListDTO);
                }
                proj.setAuthorListDTOS(strings);
            }
        }
        return topCategoryDTOList;
    }

    @Override
    public int changeStateToDelete(Long id) {
        return projMapper.changeStateToDelete(id);
    }


    /**
     *  2.0
     */
    @Override
    public PageBean<ProjListItemDTO> showMoreByCondition(Integer fid, Integer cid, Integer pageNum, Integer pageSize) {
        // 获取所有属于该父类的子类别
        List<Category> category = categoryMapper.selectCategoryByPid(fid);

        // 获取项目ID
        List<Long> temp = new ArrayList<>();
        for (Category c : category) {
            //用来放置顶级类别下所有子类别对应的项目的编号
            ArrayList<AuthorListDTO> strings = new ArrayList<>();
            List<Long> longs = projCategoryMapper.selectProjIdByCategoryId(c.getId());
            for (Long aLong : longs) {
                temp.add(aLong);
            }
        }
        // 获取属于子分类的项目ID,传子分类就查双条件下的proj,否则就查父类下的所有proj
        List<Long> longs = new ArrayList<>();
        if (!ObjectUtils.isEmpty(cid)){
            longs = projCategoryMapper.selectProjIdByCategoryId(cid.longValue());
        }else {
            longs = temp;
        }

        // 分页查询
        Page<Proj> objects = PageHelper.startPage(pageNum, pageSize);
        projMapper.selectProjByArray(longs);

        // 实体类转化
        List<Proj> result = objects.getResult();
        List<ProjListItemDTO> projListItemDTOS = ProjTrans.MAPPER.projList2ProjListItemDTOList(result);

        // 获取作者信息
        for (ProjListItemDTO projListItemDTO : projListItemDTOS) {
            ArrayList<AuthorListDTO> strings = new ArrayList<>();
            AuthorListDTO authorListDTO = AuthorTrans.MAPPER.author2AuthorListDTO(authorMapper.selectByPrimaryKey(projListItemDTO.getId()));
            strings.add(authorListDTO);
            projListItemDTO.setAuthorListDTOS(strings);
        }

        // 给pagebean赋值
        PageBean<ProjListItemDTO> pageBean = new PageBean<>();
        pageBean.setResult(projListItemDTOS);
        pageBean.setPageSize(objects.getPageSize());
        pageBean.setPageNum(objects.getPageNum());
        pageBean.setTotal(objects.getTotal());
        pageBean.setPages(objects.getPages());
        return pageBean;
    }


    /**
     * 3.0
     * 查找项目列表
     * @param fid       父类ID
     * @param cid       子类ID
     * @param pName     项目名
     * @param pageNum   页码
     * @param pageSize  页面大小
     * @return
     */
    @Override
    public PageBean<ProjListItemDTO> showAll(Integer fid, Integer cid,String pName, Integer pageNum, Integer pageSize) {
        // 获取所有属于该父类的子类别
        List<Category> category = categoryMapper.selectCategoryByPid(fid);

        // 获取项目ID
        List<Long> temp = new ArrayList<>();
        for (Category c : category) {
            //用来放置顶级类别下所有子类别对应的项目的编号
            ArrayList<AuthorListDTO> strings = new ArrayList<>();
            List<Long> longs = projCategoryMapper.selectProjIdByCategoryId(c.getId());
            for (Long aLong : longs) {
                temp.add(aLong);
            }
        }
        // 获取属于子分类的项目ID,传子分类就查双条件下的proj,否则就查父类下的所有proj
        List<Long> longs = new ArrayList<>();
        if (!ObjectUtils.isEmpty(cid)){
            longs = projCategoryMapper.selectProjIdByCategoryId(cid.longValue());
        } else if (!ObjectUtils.isEmpty(pName)){
            longs = projMapper.selectProjLikeName(pName);
        } else {
            longs = temp;
        }

        // 分页查询
        Page<Proj> objects = PageHelper.startPage(pageNum, pageSize);
        projMapper.selectProjByArray(longs);

        // 实体列表转化
        List<Proj> result = objects.getResult();
        List<ProjListItemDTO> projListItemDTOS = ProjTrans.MAPPER.projList2ProjListItemDTOList(result);

        // 获取作者信息
        for (ProjListItemDTO proj : projListItemDTOS) {
            ArrayList<AuthorListDTO> strings = new ArrayList<>();
            List<Integer> authorIds = projAuthorMapper.getProjAuthorIds(proj.getId());
            for (Integer authorId : authorIds) {
                AuthorListDTO authorListDTO = AuthorTrans.MAPPER.author2AuthorListDTO(authorMapper.selectByPrimaryKey(authorId.longValue()));
                strings.add(authorListDTO);
                proj.setAuthorListDTOS(strings);
            }
        }

        // 给pagebean赋值
        PageBean<ProjListItemDTO> pageBean = new PageBean<>();
        pageBean.setResult(projListItemDTOS);
        pageBean.setPageSize(objects.getPageSize());
        pageBean.setPageNum(objects.getPageNum());
        pageBean.setTotal(objects.getTotal());
        pageBean.setPages(objects.getPages());
        return pageBean;
    }



}